{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html\n",
    "#优酷频道：http://i.youku.com/sdxxqbf\n",
    "#微信公众号：深度学习与神经网络\n",
    "#Github：https://github.com/Qinbf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.79306585  0.93936225 -0.60257721 -0.79228528]\n",
      " [-0.33840192 -0.52791209  0.55944743 -0.40831393]\n",
      " [ 0.96380363 -0.3276025  -0.5034575  -0.09247931]]\n",
      "[[-0.17293012]\n",
      " [ 0.93869591]\n",
      " [ 0.44443866]\n",
      " [ 0.65668778]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],\n",
    "              [1,0,1],\n",
    "              [1,1,0],\n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([[0,1,1,0]])\n",
    "#权值初始化，取值范围-1到1\n",
    "V = np.random.random((3,4))*2-1 \n",
    "W = np.random.random((4,1))*2-1\n",
    "print(V)\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "\n",
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "def dsigmoid(x):\n",
    "    return x*(1-x)\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,V,lr\n",
    "    \n",
    "    L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "    L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "    \n",
    "    L2_delta = (Y.T - L2)*dsigmoid(L2)\n",
    "    L1_delta = L2_delta.dot(W.T)*dsigmoid(L1)\n",
    "    \n",
    "    W_C = lr*L1.T.dot(L2_delta)\n",
    "    V_C = lr*X.T.dot(L1_delta)\n",
    "    \n",
    "    W = W + W_C\n",
    "    V = V + V_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: 0.499047148968\n",
      "Error: 0.499857996791\n",
      "Error: 0.499614219706\n",
      "Error: 0.499316268255\n",
      "Error: 0.498682783133\n",
      "Error: 0.496841542713\n",
      "Error: 0.489461839163\n",
      "Error: 0.449512073597\n",
      "Error: 0.313798464967\n",
      "Error: 0.197238982213\n",
      "Error: 0.142094417235\n",
      "Error: 0.113103606623\n",
      "Error: 0.0954231534527\n",
      "Error: 0.0834680939614\n",
      "Error: 0.0747931048371\n",
      "Error: 0.0681739585616\n",
      "Error: 0.0629319246265\n",
      "Error: 0.0586602932735\n",
      "Error: 0.0551001604186\n",
      "Error: 0.0520785809769\n",
      "Error: 0.0494754177282\n",
      "Error: 0.0472044694425\n",
      "Error: 0.0452021783992\n",
      "Error: 0.0434205838824\n",
      "Error: 0.0418227653044\n",
      "Error: 0.0403798032797\n",
      "Error: 0.0390686980356\n",
      "Error: 0.0378709096924\n",
      "Error: 0.0367713131431\n",
      "Error: 0.0357574358059\n",
      "Error: 0.0348188923908\n",
      "Error: 0.0339469594524\n",
      "Error: 0.0331342507934\n",
      "Error: 0.0323744667475\n",
      "Error: 0.0316621983325\n",
      "Error: 0.0309927726809\n",
      "Error: 0.0303621298827\n",
      "Error: 0.0297667239918\n",
      "Error: 0.0292034428078\n",
      "Error: 0.0286695423777\n",
      "[[ 0.03097444]\n",
      " [ 0.97259213]\n",
      " [ 0.97276123]\n",
      " [ 0.02703325]]\n",
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(20000):\n",
    "    update()#更新权值\n",
    "    if i%500==0:\n",
    "        L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "        L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "        print('Error:',np.mean(np.abs(Y.T-L2)))\n",
    "        \n",
    "L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "print(L2)\n",
    "\n",
    "def judge(x):\n",
    "    if x>=0.5:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "for i in map(judge,L2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
